package com.ttbj.controller.constant;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.web.socket.WebSocketSession;

@SuppressWarnings("rawtypes")
public class WebSessionMap {

	private static Map<String, WebSocketSession> webSessionMap = new ConcurrentHashMap<String, WebSocketSession>(); // Map来存储WebSocketSession，key用USER_ID

	public static void put(String userId, WebSocketSession webSocketSession) {
		webSessionMap.put(userId, webSocketSession);
	}

	public static WebSocketSession get(String userId) {
		return webSessionMap.get(userId);
	}

	public static void remove(WebSocketSession webSocketSession) {
		for (Map.Entry entry : webSessionMap.entrySet()) {
			if (entry.getValue() == webSocketSession) {
				webSessionMap.remove(entry.getKey());
			}
		}
	}

	public static void remove(String userId) {
		webSessionMap.remove(userId);
	}

	public static String getKey(WebSocketSession webSocketSession) {
		for (Map.Entry entry : webSessionMap.entrySet()) {
			if (entry.getValue() == webSocketSession) {
				return entry.getKey().toString();
			}
		}
		return null;
	}

	public static int size() {
		return webSessionMap.size();
	}

}
